﻿<!doctype html>
<html>
<head>
	<meta charset="utf-8" />
	<title>优化标题</title>
	<link href="css/mycss.css" rel="stylesheet" type="text/css">
    <link href="css/pagination.css" rel="stylesheet" type="text/css">
    <link href="http://a.tbcdn.cn/s/kissy/1.3.0/css/dpl/??base-min.css,badges-min.css,forms-min.css,icons-min.css,labels-min.css,tables-min.css" rel="stylesheet" type="text/css">
    <link href="http://a.tbcdn.cn/s/kissy/1.3.0/??button/assets/dpl-min.css,calendar/assets/dpl-min.css,menu/assets/dpl-min.css,combobox/assets/dpl-min.css,menubutton/assets/dpl-min.css,overlay/assets/dpl-min.css,split-button/assets/dpl-min.css,tabs/assets/dpl-min.css,toolbar/assets/dpl-min.css,tree/assets/dpl-min.css" rel="stylesheet" type="text/css">
    <script src="http://a.tbcdn.cn/s/kissy/1.3.0/kissy.js" charset="utf-8"></script> 
	<script src="javascript/pagination.js"></script>
	<script src="dwr/interface/TitleService.js"></script>
	<script src="dwr/engine.js"></script>
	<script type="text/javascript">
		 KISSY.getScript("http://l.tbcdn.cn/apps/top/x/sdk.js?appkey="+KISSY.Cookie.get("appkey"));
	</script>
	<script type="text/template" id="default-pagination-tpl"> 
        {{#! 显示首页 }}
        {{#if currentPage === startPage }}
        <a class="pg-prev pg-first pg-disabled" href="#">首页</a>
        {{#else}}
        <a class="pg-prev pg-first" href="#" data-page="{{ startPage }}">首页</a>
        {{/if}}
 
        {{#! 显示上一页 }}
        {{#if currentPage === startPage }}
        <a class="pg-prev pg-disabled">上一页</a>
        {{#else}}
        <a class="pg-prev" href="#" data-page="{{ currentPage - 1 }}">上一页</a>
        {{/if}}
 
        {{#if showPageNum }}
        {{#! 左边固定显示几页, 如固定显示 1 和 2 }}
        {{#if currentPage > startPage + alwaysDisplayCount - 1}}
        {{#for var i = 0; i < alwaysDisplayCount; i++ }}
        {{#! 避免后面重复显示 }}
        {{#if i + startPage < startIndex }}
        <a class="pg-page pg-item" href="#" data-page="{{ i + startPage }}">{{ i + startPage }}</a>
        {{/if}}
        {{/for}}
        {{/if}}
 
        {{#! 是否显示省略号 }}
        {{#if ellipseText && startIndex > startPage + alwaysDisplayCount }}
        <span class="pg-item">…</span>
        {{/if}}
 
        {{#! 显示页数 }}
        {{#for var i = startIndex; i <= endIndex; i++ }}
        {{#if currentPage !== i }}
        <a class="pg-page pg-item" href="#" data-page="{{ i }}">{{ i }}</a>
        {{#else}}
        <span class="pg-current pg-item">{{ i }}</span>
        {{/if}}
        {{/for}}
 
        {{#! 是否显示省略号 }}
        {{#if ellipseText && endIndex < endPage - alwaysDisplayCount }}
        <span class="pg-item">…</span>
        {{/if}}
 
        {{#! 右边固定显示几页 }}
        {{#if currentPage < endPage - alwaysDisplayCount + 1 }}
        {{#for var i = endPage - alwaysDisplayCount + 1; i <= endPage; i++ }}
        {{#if i > endIndex }}
        <a class="pg-page pg-item" href="#" data-page="{{ i }}">{{ i }}</a>
        {{/if}}
        {{/for}}
        {{/if}}
        {{/if}}
 
        {{#! 显示下一页 }}
        {{#if currentPage === endPage }}
        <a class="pg-next pg-disabled">下一页</a>
        {{#else}}
        <a class="pg-next" href="#" data-page="{{ currentPage + 1 }}">下一页</a>
        {{/if}}
 
        {{#! 显示末页 }}
        {{#if currentPage === endPage }}
        <a class="pg-next pg-last pg-disabled" href="#">末页</a>
        {{#else}}
        <a class="pg-next pg-last" href="#" data-page="{{ endPage }}">末页</a>
        {{/if}}
    </script>
</head>
<body>
     <!--主体内容-->
     <div id="content">
         <!-- 功能描述 -->
         <div style="padding: 20px 0px;">
             <div class="ks-popup-inner">
                 <h3 class="ks-popup-title">标题优化</h3>
                 <div class="ks-popup-content">
                     <p>And here's some amazing content. It's very engaging. right?</p>
                 </div>
             </div>
         </div>
         <!-- 搜索 -->
         <div class="search">
             <form class="well form-search">
                 <span>商品搜索：</span>
                 <input id="searchTxt" type="text" class="input-medium search-query" placeholder="亲 ，输入宝贝的标题"/>
                 <a href="#"><div class="ks-button" id="search" title="亲，可以根据关键词搜索出售中的商品哟！">搜索</div></a>
                 <div id="itemAll" class="ks-button ks-button-primary" style="float: right; margin-right: 20px;">一键优化</div>
             </form>
         </div>
         <!-- 商品列表 -->
         <table class="table" id="items">
             <thead>
                 <tr>
                     <th width="40">图片</th>
                     <th width="50">价格</th>
                     <th width="50">数量</th>
                     <th width="150">上架时间</th>
                     <th width="70">橱窗推荐</th>
                     <th width="500">宝贝标题</th>
                     <th width="180">原因</th>
                     <th width="80">优化</th>
                 </tr>
             </thead>
			 <tbody class="page-content" id="page-content"></tbody>
         </table>
        <!-- 分页 -->
		<div class="pagination" id="page" style="margin-right: 20px"></div> 
		<script> 
		   KISSY.use('gallery/pagination/1.1/pagination',function(S, P) {
		   	var $ = S.all;
		   	var totalPage;
		   	searchItem("");
			function searchItem(title)
			{
				pagination = null;
				TOP.api("tql","get",{ql:"select num_iid from taobao.items.onsale.get where q="+title,session:TOP.cookie.get("session")},function(response){
		    		totalPage = response.items_onsale_get_response.total_results;
		    		var content = S.one('#page-content'),
				    pagination = new P({
				        container: '#page',
				        template: S.one('#default-pagination-tpl').html(),
				        totalPage: Math.ceil(totalPage/8),
				        callback: function(idx, pg, ready) {
				            content.html('').addClass('loading');
				            TOP.api("tql","get",{ql:"select pic_url,title,num_iid,has_showcase,num,price,delist_time from taobao.items.onsale.get where q="+ title +" and page_size=8 and page_no="+idx,session:TOP.cookie.get("session")},function(data){
				            	var html = '';
				            	S.each(data.items_onsale_get_response.items.item,function(item, i) {
				            		item.has_showcase = item.has_showcase? "已推荐":"未推荐";
				            		item.reason = 29-Math.round(item.title.replace(/[^\x00-\xff]/g, "**").length/2)<=0? "暂无建议":"标题未满30个字符";
									html += '<tr>'
						                   +    '<td style="padding-top: 8px;">'
						                   +        '<img src="'+item.pic_url+'_40x40.jpg" alt="'+item.title+'" width="40" height="40">'
						                   +    '</td>'
						                   +    '<td>'+item.price+'</td>'
						                   +    '<td>'+item.num+'</td>'
						                   +    '<td>'+item.delist_time+'</td>'
						                   +    '<td>'+item.has_showcase+'</td>'
						                   +    '<td><a href="http://item.taobao.com/item.htm?id='+ item.num_iid +'" target="_blank">'+ item.title +'</a></td>'
						                   +    '<td>'+item.reason+'</td>'
						                   +    '<td><div class="ks-button ks-button-primary" data="'+ item.num_iid +'" index="'+ i +'">优化</div></td>'
						                   +'</tr>';
				                });
			            		content.removeClass('loading').html(html);
			                    ready(idx);
			                    $("#page-content .ks-button").on('click',function(e){
			                    	var button = e.currentTarget;
			                    	var num_iid = button.getAttribute('data');
			                    	var index = button.getAttribute('index');
			                    	TitleService.update(num_iid,function(result){
			                    		if(result.succeed){
			                    			S.get('#page-content').rows[index].cells[5].innerHTML = '<a href="http://item.taobao.com/item.htm?id='+num_iid+'" target="_blank">'+result.result+'</a>';
			                    			S.get('#page-content').rows[index].cells[6].innerHTML = '暂无建议';
			                    		}
			                    		else{
			                    			var message;
			                    			S.each(result.errList,function(item){
			                    				message += item;
			                    			});
			                    			alert(message);
			                    		}
			                    	});
				                });
				            });
				        }
				    });
		    	});
			}
			//点击搜索
			$('#search').on('click', function(e) {
				if($('#searchTxt').val())
				{
					$('#search').text("全店商品");
				}
				else{
					$('#search').text("搜索");
				}
				searchItem($('#searchTxt').val());
				$('#searchTxt').val("");
			});
			
			$('#itemAll').on('click', function(event) {
				TitleService.updateAll(function(){
					searchItem('');
				});
			});
		});
		</script> 
     </div>
</body>
</html>
